package com.yami.shop.mystery.api.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.shop.bean.event.NewcomerRewardsEvent;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.mystery.common.model.ActivityPresell;
import com.yami.shop.mystery.common.model.ActivityProd;
import com.yami.shop.mystery.common.model.InviteRecord;
import com.yami.shop.mystery.common.service.ActivityPresellService;
import com.yami.shop.mystery.common.service.ActivityProdService;
import com.yami.shop.mystery.common.service.InviteRecordService;
import com.yami.shop.mystery.common.vo.NewcomerRewardsVo;
import com.yami.shop.mystery.common.vo.ProdVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/p/activityPresell")
public class ActivityPresellController {

    @Autowired
    private ActivityPresellService activityPresellService;
    @Autowired
    private ActivityProdService prodService;
    @Autowired
    private InviteRecordService inviteRecordService;
    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 功能描述: 弹窗领取活动
     * @Author: LSR
     * @Date: 2023/9/4 18:24
     */
    @GetMapping("/newcomerRewards")
    public ServerResponseEntity<NewcomerRewardsVo> newcomerRewards(String activityType, String userId){
        NewcomerRewardsVo newcomerRewardsVo = new NewcomerRewardsVo();
        // 获取活动信息
        QueryWrapper<ActivityPresell> activityPresellQueryWrapper = new QueryWrapper<>();
        activityPresellQueryWrapper.eq("activity_type",activityType);
        activityPresellQueryWrapper.eq("del_flag",0);
        activityPresellQueryWrapper.eq("status",0);
        activityPresellQueryWrapper.le("activity_end_time",new Date());
        activityPresellQueryWrapper.ge("activity_start_time",new Date());
        ActivityPresell activityPresell = activityPresellService.getOne(activityPresellQueryWrapper);
        if(ObjectUtil.isEmpty(activityPresell)){
            return ServerResponseEntity.success();
        }
        newcomerRewardsVo.setActivityName(activityPresell.getActivityName());
        // 获取商品信息
        List<ActivityProd> prods = prodService.listByActivityId(activityPresell.getId(),activityPresell.getBlindType());
        // 查询是否领取过
        List<ProdVo> prodVos = new ArrayList<>();
        newcomerRewardsVo.setIsRewards(false);
        for(ActivityProd activityProd : prods){
            ProdVo prodVo = new ProdVo();
            prodVo.setProdName(activityProd.getProdName());
            prodVo.setProdImg(activityProd.getPic());
            prodVos.add(prodVo);
            if(ObjectUtil.isNotEmpty(userId)){
                // 查询是否领取过
                QueryWrapper<InviteRecord> inviteRecordQueryWrapper = new QueryWrapper<>();
                inviteRecordQueryWrapper.eq("user_id",userId);
                inviteRecordQueryWrapper.eq("activity_id",activityPresell.getId());
                inviteRecordQueryWrapper.eq("prod_id",activityProd.getProdId());
                InviteRecord one = inviteRecordService.getOne(inviteRecordQueryWrapper);
                if(ObjectUtil.isNotEmpty(one)){
                    newcomerRewardsVo.setIsRewards(true);
                }
            }
        }
        newcomerRewardsVo.setProdVos(prodVos);
        return ServerResponseEntity.success(newcomerRewardsVo);
    }

    /**
     * 功能描述: 领取活动--领取
     * @Author: LSR
     * @Date: 2023/9/5 11:29
     */
    @GetMapping("/receive")
    public ServerResponseEntity<Void> receive(String userId,Long activityId){
        if(ObjectUtil.isEmpty(userId)){
            return ServerResponseEntity.showFailMsg("参数异常");
        }
        applicationContext.publishEvent(new NewcomerRewardsEvent(userId,"",activityId,"2"));
        return ServerResponseEntity.success();
    }
}
